/**
 * 功能描述
 */

define(function (require, exports, module) {
	var Subnav = require("subnav");
	var aw = require("ajaxwrapper");
	var Dialog = require("dialog");
	var Grid = require("grid");
	var datas;
	var index ;
    var instance = {
        initComponent: function (params,widget) {
        	var subnav = new Subnav({
        		parentNode:".J-subnav",
        		model:{
        			title:"调度管理",
        			items:[{
						id:"search",
						type: "search",
						placeholder: "组团单位/司机名称",
						handler: function(str){
							aw.ajax({
								url: "api/cardispatch/searchcarbill",
								data: {
									s: str,
									searchProperties: "driverName,driver.personalInfo.name,nonMemGroupReserve.nonMemberGroup.contactName,groupReserve.planItemGroup.allianceOrg.name,groupReserve.planItemGroup.servicePoint.name",
									fetchProperties:"*,car.carType,car.carNumber,driver.personalInfo.name,driver.personalInfo.phone," +
        							"groupReserve.*,nonMemGroupReserve.*,nonMemGroupReserve.nonMemberGroup.*," +
        							"groupReserve.planItemGroup.*," +
        							"groupReserve.planItemGroup.allianceOrg.*," +
        							"nonMemGroupReserve.nonMemberGroup.*," +
        							"nonMemGroupReserve.nonMemberGroup.servicePoint.*"
								},
								success: function(data){
									widget.get("grid").setData(data);
								}
							});
						}
            		},{
        				id:"status",
        				type:"buttongroup",
        				tip:"状态",
        				items:[{
        					key:"Initial",
        					value:"初始"
        				},{
        					key:"Sent",
        					value:"已派车"
        				},{
        					key:"Back",
        					value:"已收车"
        				}],
	        			all: {
	    					show: true,
	    					position: "top"
	    				},
	    				handler:function(key,element){
							widget.get("grid").refresh();
						}
        			},{
            			id:"carDate",
						tip:"用车时间",
						type:"daterange",
						ranges:{
					 		"本月": [moment().startOf("month"), moment().endOf("month")],
					 		"上月": [moment().subtract(1,"months").startOf("month"), moment().subtract(1,"months").endOf("month")],
					 		"本周": [moment().startOf("week"), moment().endOf("week")],
					 		"上周": [moment().subtract(1,"week").startOf("week"), moment().subtract(1,"week").endOf("week")]
						},
						defaultRange : "本月",
						handler : function(time){
							widget.get("grid").refresh();
						}
            		},{
            			id:"return",
    					type:"button",
    					text:"返回",
    					show:false,
    					handler:function(){
    						widget.show([".J-grid"]).hide([".J-form"]);
							widget.get("subnav").hide(["return"]).show(["search","add","carDate","status"]);
    					}
            		},{
        				id: "add",
        				type: "button",
        				text: "新增",
        				handler: function(){
					        var grid = new Grid({
					          autoRender: false,
					          model: {
				        			url:"api/checkinimplement/querynostatementteam",
				        			params:function(){
				        				var subnav = widget.get("subnav");
				        				return{
				        					fetchProperties:"pkCIImplement,dataSource,statementStatus,version,groupReserve.planItemGroup.allianceOrg.name," +
				        							"groupReserve.planItemGroup.checkInStatus,nonMemGroupReserve.nonMemberGroup.status," +
				        							"groupReserve.planItemGroup.servicePoint.name," +
				        							"groupReserve.checkInDate,groupReserve.checkOutDate," +
				        							"groupReserve.planItemGroup.planItem.destination.name," +
				        							"signUpNum,groupReserve.pkGroupReserve," +
				        							"nonMemGroupReserve.pkNonMemGroupReserve," +
				        							"nonMemGroupReserve.nonMemberGroup.contactName," +
				        							"nonMemGroupReserve.nonMemberGroup.servicePoint.name," +
				        							"nonMemGroupReserve.nonMemberGroup.checkInDate," +
				        							"nonMemGroupReserve.nonMemberGroup.checkOutDate," +
				        							"nonMemGroupReserve.nonMemberGroup.perNumber" 
				        							 
				        				}
				        			},
				        			columns:[{
				        				name:"groupReserve",
				        				label:"组团单位/预约人",
				        				format: function(value,row){
				        					if(row.dataSource.key == "GroupReserve"){
				        						if(value.planItemGroup.allianceOrg){
				        							return value.planItemGroup.allianceOrg.name;
				        						}else{
				        							return value.planItemGroup.servicePoint.name
				        						}
				        					}else{
				    							return row.nonMemGroupReserve.nonMemberGroup.contactName==null?" ":row.nonMemGroupReserve.nonMemberGroup.servicePoint.name;
				        					}
				        				}
				        			},{
				        				name:"destinationName",
				        				label:"目的地",
				        				format : function(value, row){
				        					if(row.dataSource.key == "GroupReserve"){
				        						return row.groupReserve.planItemGroup.planItem.destination.name;
				        					}else{
				        						return row.nonMemGroupReserve.nonMemberGroup.servicePoint == null ? "" : row.nonMemGroupReserve.nonMemberGroup.servicePoint.name;
				        					}
				        				}
				        			},{
				        				name:"groupReserve.checkInDate",
				        				label:"开始日期",
				        				format:"date",
				        				format:function(value,row){
				        					if(row.dataSource.key == "GroupReserve"){
				        						return moment(value).format("YYYY-MM-DD");
				        					}else{
				        						return moment(row.nonMemGroupReserve.nonMemberGroup.checkInDate).format("YYYY-MM-DD");
				        					}
				        				}
				        			},{
				        				name:"groupReserve.checkOutDate",
				        				label:"结束日期",
				        				format:"date",
				        				format:function(value,row){
				        					if(row.dataSource.key == "GroupReserve"){
				        						return moment(value).format("YYYY-MM-DD");
				        					}else{
				        						return moment(row.nonMemGroupReserve.nonMemberGroup.checkOutDate).format("YYYY-MM-DD");
				        					}
				        				}
				        			},{
				        				name:"signUpNum",
				        				label:"报名人数",
				        				format:function(value,row){
				        					if(row.dataSource.key == "GroupReserve"){
				        						return row.signUpNum;
				        					}else{
				        						return row.nonMemGroupReserve.nonMemberGroup.perNumber==null? 0 :row.nonMemGroupReserve.nonMemberGroup.perNumber;
				        					}
				        				}
				        			}],
				        			isRadiobox:true,
				        			items_per_page: 10,
				        			clickRadiobox: function(e){
				        				datas =grid.getSelectedData();
				        			   }
				        		}
					    	});
					        Dialog.showComponent(grid,{
					        	defaultButton:false,
								title: "请选择用车单位",
								setStyle: function(){
									$(".el-dialog-modal .modal").css({
										"top": "8%",
										"width":"70%",
										"left": "10%",
										"bottom":"10%"
									});
								},
					        	buttons :[{
									id:"save",
									text:"确定",
 									className:"btn-primary",
									handler:function(index,data,rowEle){
										form = widget.get("form");
			        					form.reset();
			        					Dialog.close();
										widget.show([".J-form"]).hide([".J-grid"]);
										widget.get("subnav").hide(["search","add","carDate","status"]);
										form.hide(["companyName","carModel","driverName"]);
										for(var i=0;i<datas.length;i++){
											if(datas[i].dataSource.key!="GroupReserve"){
												form.setValue("nonMemGroupReserve", datas[i].nonMemGroupReserve.pkNonMemGroupReserve);
		    									form.setValue("groupReserveN",datas[i].nonMemGroupReserve.nonMemberGroup.contactName);
		    									form.setValue("destination",datas[i].nonMemGroupReserve.nonMemberGroup.servicePoint.name);
		    									form.setValue("checkInDate",moment(datas[i].nonMemGroupReserve.nonMemberGroup.checkInDate).format("YYYY-MM-DD"));
		    									form.setValue("checkOutDate",moment(datas[i].nonMemGroupReserve.nonMemberGroup.checkOutDate).format("YYYY-MM-DD"));
		    									form.setValue("signUpNum",datas[i].signUpNum);
		    								}else{
		    									form.setValue("groupReserve",datas[i].groupReserve.pkGroupReserve);
		    									form.setValue("groupReserveN",datas[i].groupReserve.planItemGroup.planItem.destination.name);
		    									form.setValue("destination",datas[i].groupReserve.planItemGroup.servicePoint.name);
		    									form.setValue("checkInDate",moment(datas[i].groupReserve.checkInDate).format("YYYY-MM-DD"));
		    									form.setValue("checkOutDate",moment(datas[i].groupReserve.checkOutDate).format("YYYY-MM-DD"));
		    									form.setValue("signUpNum",datas[i].signUpNum);
		    								}
										}
									}
								},{
									id:"close",
									text:"取消",
									handler:function(){
										Dialog.close();
										widget.get("subnav").show(["search","add","carDate","status"]);
									}
								}]
					        });
					        grid.refresh();
        				}
        			}]
        		}
        	});
        	widget.set("subnav",subnav);
        }
    };
    module.exports = instance;
});
